<?php
namespace Wechat\Model;
use Think\Model;


class StudentInfoModel  {
    
    private $user ,$passwd ;
    public function updateInfo($key){
//        echo "\n==>$key<==\n";
        $query = new \Wechat\Model\StudentModel();
        $chk = $query->chkPublicKey("".$key);
//        print_r($chk);
        $query = new \Wechat\Model\DataqueryModel();
        $query->setKey($key);
        if ($chk!=NULL){
            $this->user = $chk['stunumber'];
            $this->passwd = $chk['passwd'];
//            $this->updateClass($key);
//            $this->updateExam($key);
//            $this->updateGrade($key);
            if ($query->login($this->user, $this->passwd)){
                echo "ok";
                //登录成功
//                $query->setKey($key);
                $query->jwCheck();
                //跳转到教务
                $query->getClassInfo();
                //获取课表
                $examinfo = $query->getExam();
                if(($examinfo!="")and($examinfo!=null)){
                    $exa = new \Wechat\Model\ExaModel();
                    $exa->setExa($key, $examinfo);
                }
                //获取考试
                $gradeinfo = $query->getMark();
                $flag = 0;
                if (($gradeinfo==null)or ($gradeinfo=="")) {
                    $gradeinfo = "暂无成绩记录";
                    $flag = 1;
                }
                $gra = new \Wechat\Model\GraModel();
                $gra->setGra($key, $gradeinfo,$flag);
                //获取成绩
            }else{
                echo "passwd error";
            }
        }else{
            return NULL;
        }
    }
    
    public function updateg($key){
        $query = new \Wechat\Model\StudentModel();
        $chk = $query->chkPublicKey("".$key);
        if ($chk!=NULL){
            $this->user = $chk['stunumber'];
            $this->passwd = $chk['passwd'];
            $tmp = $this->updateGrade($key);
//            echo $tmp;
        }else{
            return NULL;
        }
    }


    private function updateClass($key){
        $query = new \Wechat\Model\DataqueryModel();
        $query->setKey($key);
        if ($query->login($this->user, $this->passwd)){
            $query->jwCheck();
            $query->getClassInfo();
            return true;
        }
    }
    
    private function updateExam($key){
        $query = new \Wechat\Model\DataqueryModel();
        $query->setKey($key);
        if ($query->login($this->user, $this->passwd)){
            $query->jwCheck();
            $examinfo = $query->getExam();
            if(($examinfo!="")and($examinfo!=null)){
                $exam = new \Wechat\Model\ExamModel();
                $exam->saveExam($key, $examinfo);
            }
           
            return true;
        }
    }
    
    private function updateGrade($key){
        $query = new \Wechat\Model\DataqueryModel();
        $query->setKey($key);
        if ($query->login($this->user, $this->passwd)){
            $query->jwCheck();
            $gradeinfo = $query->getMark();
            if (($examinfo==null)or ($examinfo=="")) $examinfo = "暂无本学期成绩记录";
//            echo $examinfo;
            $grade = new \Wechat\Model\GradeModel();
            $grade->saveGrade($key, $gradeinfo);
            return $gradeinfo;
        }
    }
    
    
}
